﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace NCAB.WF
{
    public partial class Default : System.Web.UI.Page
    {
        private MembershipUser user;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
            }

            if (Request.IsAuthenticated && Membership.GetUser() != null)
            {
                Response.Redirect("Home.aspx");
            }
        }

        void Page_PreRender(object obj, EventArgs e)
        {
            ViewState["update"] = Session["update"];
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            HtmlInputText usuario = (HtmlInputText)HeadLoginView.Controls[0].Controls[0].FindControl("formLogin").FindControl("inputUsuario");
            HtmlInputText password = (HtmlInputText)HeadLoginView.Controls[0].Controls[0].FindControl("formLogin").FindControl("inputPassword");

            if (Session["update"].ToString() == ViewState["update"].ToString())
            {  //--- Insert data execute here

                bool isValid = Membership.ValidateUser(usuario.Value.ToString(), password.Value.ToString());

                if (!isValid)
                {
                    user = Membership.GetUser(usuario.Value.ToString());
                    if (user != null)
                    {
                        /* User exists */
                        if (!user.IsApproved)
                        {
                            ScriptManager.RegisterStartupScript(HeadLoginView, HeadLoginView.GetType(), "scriptUnapproved", "notificacion('error', 'Usuario no aprobado.');", true);
                        }
                        else if (user.IsLockedOut)
                        {
                            ScriptManager.RegisterStartupScript(HeadLoginView, HeadLoginView.GetType(), "scriptLocked", "notificacion('error', 'Usuario bloqueado.');", true);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(HeadLoginView, HeadLoginView.GetType(), "scriptInvalid", "notificacion('error', 'Usuario y/o Password inválidos.');", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(HeadLoginView, HeadLoginView.GetType(), "scriptInvalid", "notificacion('error', 'Usuario y/o Password inválidos');", true);
                    }
                }
                else
                {
                    /* successful login */
                    FormsAuthentication.RedirectFromLoginPage(usuario.Value.ToString(), true);
                    Response.Redirect("Home.aspx");
                }
                //--End Insert
                Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
            }
            else
            {
                //EN CASO DE QUE LA PAGINA SEA ACTUALIZADA
                usuario.Value = "";
                password.Value = "";
                usuario.Focus();
            }
        }
    }
}